package patrol.mobile.com.mobilepatrol.activity.toll;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.TollAndServiceDetailBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

public class TollAndServiceDetailActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.iv_pic)
    ImageView ivPic;
    private String title;
    private String area_id;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_toll_and_service_detail;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(TollAndServiceDetailActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        title = getIntent().getStringExtra(TollAndServiceAreaActivity.TITLE);
        area_id = getIntent().getStringExtra(TollAndServiceAreaActivity.AREA_ID);
        tv_center_title.setText(title + "详情");
        initWebView();
    }

    @Override
    public void initData() {
        super.initData();
        if (title.equals("收费站信息")) {
            //收费站信息
            requestData(Urls.GET_ROUTE_TOLL_GATE_ENTITY);
        } else {
            //服务区
            requestData(Urls.GET_ROUTE_SERVICE_AREA_ENTITY);
        }
    }

    private void requestData(String url) {
        HttpSender sender = new HttpSender(this, baseUrl, url);
        sender.requestPost("id", area_id, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                TollAndServiceDetailBean tollAndServiceDetailBean = GsonUtil.GsonToBean(response, TollAndServiceDetailBean.class);
                tvTitle.setText(tollAndServiceDetailBean.getTitle());
                Glide.with(TollAndServiceDetailActivity.this).load(tollAndServiceDetailBean.getImage()).into(ivPic);
                tvTime.setText(tollAndServiceDetailBean.getCreateDate());
                webview.loadData(tollAndServiceDetailBean.getContent(), "text/html;charset=utf-8", "utf-8");
            }
        });
    }

    private void initWebView() {
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 设置可以支持缩放
        settings.setSupportZoom(true);
        // 设置出现缩放工具
        settings.setBuiltInZoomControls(true);
        //不显示webview缩放按钮
        settings.setDisplayZoomControls(true);
        //扩大比例的缩放
        settings.setUseWideViewPort(true);
        settings.setTextZoom(350);
//        settings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);// 默认缩放模式
    }

    @Override
    public void setListener() {

    }
}
